package com.chase.passwordmaster.mine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.chase.passwordmaster.R
import com.chase.passwordmaster.config.SP_KEY_SETTING_OPTIONAL_SEARCH
import com.chase.passwordmaster.config.SP_KEY_SETTING_PURE_PASS_BACKGROUND
import com.chase.passwordmaster.data.AccountViewModel
import com.chase.passwordmaster.mine.export.ExportActivity
import com.chase.passwordmaster.mine.export.ImportActivity
import com.chase.passwordmaster.mine.export.removeExportedFiles
import com.chase.passwordmaster.mine.setting.pass.SettingPassActivity
import com.chase.passwordmaster.utils.*

class MineActivity : AppCompatActivity() {
    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, MineActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var accountViewModel: AccountViewModel

    private lateinit var spUtils: SPUtils

    private lateinit var cpBack: View
    private lateinit var cpStatisticTotal: TextView
    private lateinit var cpStatisticWebsite: TextView
    private lateinit var cpSettingPass: View
    private lateinit var cpExport: View
    private lateinit var cpImport: View

    private lateinit var cpOptionalSearch: Switch
    private lateinit var cpPurePassBG: Switch

    private lateinit var cpClean: View
    private lateinit var cpAbout: View
    private lateinit var cpHelp: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mine)

        accountViewModel = ViewModelProviders.of(this).get(AccountViewModel::class.java)
        spUtils = SPUtils(this, "pmConfigs")

        cpBack = findViewById(R.id.back_mine)
        cpStatisticTotal = findViewById(R.id.static_total_mine)
        cpStatisticWebsite = findViewById(R.id.static_website_mine)
        cpSettingPass = findViewById(R.id.setting_pass_mine)
        cpExport = findViewById(R.id.export_mine)
        cpImport = findViewById(R.id.import_mine)

        cpOptionalSearch = findViewById(R.id.setting_optional_search)
        cpPurePassBG = findViewById(R.id.setting_pure_pass_bg)

        cpClean = findViewById(R.id.clean_mine)
        cpAbout = findViewById(R.id.about_mine)
        cpHelp = findViewById(R.id.help_mine)

        useDarkStatusBar()
        useImmersingNavigator()

        initCPBack(cpBack = cpBack)
        initCPStatistic(cpTotal = cpStatisticTotal, cpWebsite = cpStatisticWebsite)
        initCPSetting()
        initCPExportImport()
        initOptionalSearch()
        initPurePassBG()
        initCPOthers()
        initCPClean()

        accountViewModel.getAccountLiveData().observe(this, Observer {
            cpStatisticTotal.text = accountViewModel.getAccountSize().toString()
            cpStatisticWebsite.text = accountViewModel.getWebsiteSize().toString()
        })
    }

    private fun initCPClean() {
        fun cleanFiles() {
            removeExportedFiles()
            Logger.delFile()
        }

        cpClean.setOnClickListener {
            try {
                cleanFiles()
                showToast("清理完毕")
            } catch (e : Exception) {
                Logger.e("cleanFiles", e.getContent())
                e.printStackTrace()
                showToast(e.getContent(), Toast.LENGTH_LONG)
            }
        }
    }

    private fun initOptionalSearch() {
        val isOptional = spUtils.getBoolean(SP_KEY_SETTING_OPTIONAL_SEARCH, false)
        cpOptionalSearch.isChecked = isOptional
        cpOptionalSearch.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                spUtils.putBoolean(SP_KEY_SETTING_OPTIONAL_SEARCH, isChecked)
            } else {
                spUtils.putBoolean(SP_KEY_SETTING_OPTIONAL_SEARCH, isChecked)
            }
        }
    }

    private fun initPurePassBG() {
        val isOptional = spUtils.getBoolean(SP_KEY_SETTING_PURE_PASS_BACKGROUND, false)
        cpPurePassBG.isChecked = isOptional
        cpPurePassBG.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                spUtils.putBoolean(SP_KEY_SETTING_PURE_PASS_BACKGROUND, isChecked)
            } else {
                spUtils.putBoolean(SP_KEY_SETTING_PURE_PASS_BACKGROUND, isChecked)
            }
        }
    }

    private fun initCPExportImport() {
        cpExport.setOnClickListener {
            ExportActivity.actionStart(this)
        }

        cpImport.setOnClickListener {
            ImportActivity.actionStart(this)
        }
    }

    private fun initCPBack(cpBack: View) {
        cpBack.setOnClickListener { onBackPressed() }
    }

    private fun initCPStatistic(cpTotal: TextView, cpWebsite: TextView) {
        cpTotal.text = accountViewModel.getAccountSize().toString()
        cpWebsite.text = accountViewModel.getWebsiteSize().toString()
    }

    private fun initCPSetting() {
        cpSettingPass.setOnClickListener { SettingPassActivity.actionStart(this) }
    }

    private fun initCPOthers() {
        cpAbout.setOnClickListener { AboutActivity.actionStart(this) }
        cpHelp.setOnClickListener { HelpActivity.actionStart(this) }
    }

    data class ExportInfo(
        val dir: String,
        val fileName: String,
        val content: Any
    )
}